package com.edu.admin.service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysSubject;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.SubjectMapper;
import com.edu.admin.vo.SubjectVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 学段服务
 * 
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class SubjectService extends BaseService<SubjectMapper, SysSubject> {
	
	/**
	 * 获取学科列表 (分页)
	 * 
	 * @param phaseId
	 * @param pageNum
	 * @param pageSize
	 * 
	 * @return PageInfo<SubjectVo>
	 */
	public PageInfo<SubjectVo> getSubjectListPage(Long phaseId, Integer pageNum, Integer pageSize) {
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<SubjectVo> subjectList =  this.mapper.getSubjectList(phaseId);
		
		PageInfo<SubjectVo> pageInfo = new PageInfo<SubjectVo>(subjectList);
		
		return pageInfo;
	}
	
	/**
	 * 获取学科列表
	 * 
	 * @param 
	 * @return List<SubjectVo>
	 */
	public List<SubjectVo> getSubjectList(Long phaseId) {
		return this.mapper.getSubjectList(phaseId);
	}
	

	/**
	 * 根据学段及学科名获取学科id列表
	 * 
	 * @param phaseId
	 * @param subjectNameList
	 * 
	 * @return List<Long>
	 */
	public List<Long> getSubjectIdListBySubjectNameList(Long phaseId,List<String> subjectNameList) {
		String subjectNames="";
		for (String subjectName: subjectNameList)
		{
			subjectNames = subjectNames + subjectName +",";
		}
		
		return this.mapper.getSubjectIdListBySubjectNameList(phaseId,subjectNames);
	}
	
	/**
     * 学科重复性检查
     * 
     * @param SysSubject subject
     * 
     * @return List<SubjectVo>
     * @throws Exception
     */
    public List<SubjectVo> checkSubject(SysSubject subject) throws Exception {
    	
    	return this.mapper.findSubjectBySubjectName(subject.getPhaseId(),subject.getSubjectId(), subject.getSubjectName().trim());
  
    }
	
	/**
	 * 学科保存
	 * 
	 * @param SysSubject subject
	 * 
	 * @return Map<String, Object>
	 * 
	 * @throws Exception 
	 */
	public Map<String, Object> saveSubject(SysSubject subject) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<SubjectVo> subjectList = checkSubject(subject);
		
		if (subjectList!=null && subjectList.size()>0)
		{
			throw new SysException(ExceptionEnum.SUBJECTNAMEREPEAT);
		}


		Long subjectId = subject.getSubjectId();

		if (subjectId == null) {
				
			insert(subject);
			
			subjectId = subject.getSubjectId();

		} else {
		
			updateByPrimaryKeySelective(subject);

		}
		
		result.put("subjectId", subjectId);

		return result;

	}
	
	/**
	 * 获取学科信息
	 * 
	 * @param subjectId
	 * 
	 * @return SubjectVo
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public SubjectVo getSubjectById(Long subjectId) throws IllegalAccessException, InvocationTargetException{
		
		SubjectVo subjectVo = this.mapper.getSubjectBySubjectId(subjectId);
		
		return subjectVo;
	}
	
	/**
	 * 删除学段
	 * 
	 * @param subjectId
	 * 
	 * @return int
	 */
	public int deleteSubject(Long subjectId) {
		
		if (subjectId != null) {
			return this.deleteByPrimaryKey(subjectId);

		}
		
		return 0;	

	}


}
